package BasicEntity
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ConvolutionFilter;
	public class WanderRadar extends Sprite
	{
		public var p:Shape = new Shape();
		public var xp:Number = 0;
		public var yp:Number = 0;
		public var r:Number = 0;
		
		private var wanderDistance:Number = 100 ;
		private var wanderJitter:Number = 120;
		private var wanderTarget:Vector2D = new Vector2D(r, 0);
		
				
		public function WanderRadar(_x:Number,_y:Number,_r:Number)
		{
			p = new Shape();
			xp = _x;
			yp = _y;
			r  = _r;
			DrawRadar();	     			
		}
		
		public function DrawRadar():void
		{
			var child:Shape = new Shape();
			child.graphics.lineStyle(2, 0x000000);
			child.graphics.drawCircle(xp, yp, r);
			child.graphics.endFill();
			addChild(child);
			
			p.graphics.lineStyle(0, 0xffccdd);
			p.graphics.beginFill(0xff0000, 1);
			p.graphics.drawCircle(0, -200, 20);
			p.graphics.endFill();
			addChild(p);
		}
		
		
		public function Wander():Vector2D
		{
			var v:Vector2D = new Vector2D(0,0);
			var rd:Number = Math.random() * Math.PI * 2;
			v.Length = r;
			v.Angle = rd;		
			
			p.x = v.X;
			p.y = v.Y;
			
			var targetLocal:Vector2D = v.Addition(new Vector2D(0, 0));
			var targetWorld:Vector2D = targetLocal.Addition(new Vector2D(this.x,this.y));
			return targetWorld.Normalize().CrossProduct(0.5); 
			
		}
		
		
		public function getY(_x:Number):Number
		{
			if (x > xp - r && x < xp + r)
			{
			   return(Math.sqrt(r * r - x * x));
			}
			else
			{
				trace("给定X坐标超出圆的范围");
				return 0;
			}
		}
		
		public function moveP(_x:Number):void
		{
			var y:Number = getY(_x);
			p.x = _x;
			p.y = y;
		}
		
	}
}